import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:material_collection_app/utils/network_util.dart';
import 'package:material_collection_app/utils/utils.dart';
import 'package:material_collection_app/widget/common/network_tips.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class Refresh extends StatefulWidget {
  final RefreshController refreshController;
  final Function onRefresh;
  final Function onLoading;
  final Widget child;
  final bool enablePullUp;
  final double headBottom;
  final bool enablePullDown;

  Refresh({
    Key key, 
    this.refreshController,
    this.onRefresh,
    this.onLoading,
    this.child,
    this.enablePullUp = true,
    this.enablePullDown = true,
    this.headBottom = 0
  }) : super(key: key);

  @override
  State<StatefulWidget> createState() => MyState();
}

class MyState extends State<Refresh> {

  Widget _buildChild() {
    String networkStatus = NetworkUtil.networkStatus;

    if(networkStatus == 'noConnectNetwork') {
      return NetworkTips(tips: '似乎与互联网断开连接');
    }else if(networkStatus == 'overtime') {
      return NetworkTips(tips: '网络连接超时');
    } else {
      return widget.child;
    }
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(context, width: 750, height: 1334, allowFontScaling: false);
    // widget.refreshController = RefreshController();

    return SmartRefresher(
      enablePullDown: widget.enablePullDown,
      enablePullUp: widget.enablePullUp,
      cacheExtent: 10,
      header: ClassicHeader(
        height: 120,
        outerBuilder: (child) => Column(
          children: <Widget>[
            Image.asset('assets/refresh.gif'),
            Container(
              padding: EdgeInsets.only(bottom: widget.headBottom),
              child: Padding(
                padding: EdgeInsets.only(top: 10),
                child: child,
              ),
            )
          ],
        ),
        idleText: '下拉刷新',
        idleIcon: Container(),
        releaseText: '松开刷新',
        releaseIcon: Container(),
        refreshingText: '更新中',
        refreshingIcon: Container(),
        completeText: '刷新完成',
        completeIcon: Container(),
        completeDuration: Duration(milliseconds: 500),
        textStyle: TextStyle(
          color: Utils.hexToColor('#999999'),
          fontSize: 12
        ),
        // refreshStyle: RefreshStyle.Behind,
      ),
      footer: CustomFooter(
        height: 70.w,
        builder: (BuildContext context, LoadStatus mode){
          Widget body ;

          if(mode == LoadStatus.idle){
            // body = Text("上拉加载", style: TextStyle(
            //   color: Utils.hexToColor('#999999'),
            //   fontSize: 13
            // ));
          }else if(mode==LoadStatus.loading){
            body = CupertinoActivityIndicator();
          }else if(mode == LoadStatus.failed){
            body = Text("加载失败 单击重试!", style: TextStyle(
              color: Utils.hexToColor('#999999'),
              fontSize: 13
            ));
          }else if(mode == LoadStatus.canLoading){
            body = Text("松开加载更多", style: TextStyle(
              color: Utils.hexToColor('#999999'),
              fontSize: 13
            ));
          }else if(mode == LoadStatus.noMore){
            body = Text("没有更多数据了", style: TextStyle(
              color: Utils.hexToColor('#999999'),
              fontSize: 13
            ));
          }
          return Container(
            height: 35.w,
            child: Center(child: body),
          );
        },
      ),
      controller: widget.refreshController,
      onRefresh: widget.onRefresh,
      onLoading: widget.onLoading,
      child: _buildChild()
    );
  }
}